package com.ahdy.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

// 7. 每日用药任务实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "daily_medication_tasks",
        uniqueConstraints = @UniqueConstraint(columnNames = {"planId", "taskDate", "timingType"}))
public class DailyMedicationTask {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull(message = "用药计划ID不能为空")
    @Column(nullable = false)
    private Integer planId;

    @NotNull(message = "老人ID不能为空")
    @Column(nullable = false)
    private Integer userId;

    @NotNull(message = "药物ID不能为空")
    @Column(nullable = false)
    private Integer medicationId;

    @NotNull(message = "任务日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Column(nullable = false)
    private LocalDate taskDate;

    @NotNull(message = "服药时段不能为空")
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private TimingType timingType;

    @NotNull(message = "计划服药时间不能为空")
    @JsonFormat(pattern = "HH:mm:ss")
    @Column(nullable = false)
    private LocalTime scheduledTime;

    @NotBlank(message = "剂量不能为空")
    @Size(max = 20, message = "剂量长度不能超过20字符")
    @Column(nullable = false, length = 20)
    private String dosage;

    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "ENUM('未服用','已服用','已跳过','延迟服用') DEFAULT '未服用'")
    private TaskStatus status = TaskStatus.未服用;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(updatable = false)
    private LocalDateTime createdAt;

    @PrePersist
    protected void onCreate() {
        createdAt = LocalDateTime.now();
    }

    public enum TimingType {
        早上, 中午, 晚上, 睡前
    }

    public enum TaskStatus {
        未服用, 已服用, 已跳过, 延迟服用
    }
}
